/**
 * 
 */
package com.johnny.xia.filetest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.johnny.xia.util.file.AppendToFile;

/**
 * @author johnnyxia
 *
 */
public class MemberIp{
    
   static String logTime = "2017-01-01 ";
    
    public static void main(String[] args){
        
        Map<String,Set<String>> map = new HashMap<>();
        
        for(int i=0;i<1;i++){
            String file = "daily.log.2017-01-02.log";
            String filePath="daily.log.2017-01-02_"+i+".log\\";
            String path = "D:\\mclog\\tmp\\"+filePath;
            String path2 = "D:\\mclog\\tmp\\bak\\"+filePath;
            String fileStr = readFileByLines(path+file,map);
//            AppendToFile.appendMethodA(path2+file, fileStr);  
        }
        
        for(Entry<String, Set<String>> entry : map.entrySet()){
            if(!entry.getKey().equals("1") && entry.getValue().size() > 1){
                
                Set<String> ips = entry.getValue();
                for(String ip: ips){
                    System.out.println(entry.getKey()+"-->"+ip);
                }
            }
        }
        
        
//        String file = "catalina.out-20170101";
//        String filePath="catalina.out-20170101_3\\";
//        String path = "D:\\mclog\\20170102\\"+filePath;
//        String path2 = "D:\\mclog\\20170102\\20170101_log\\"+filePath;
//        String fileStr = readFileByLines(path+file);
////        fw(path2+file, fileStr);
//        AppendToFile.appendMethodA(path2+file, fileStr);
        
//        String s = "    at org.owasp.esapi.reference.validation.StringValidationRule.checkLength(StringValidationRule.java:214)";
        
//        System.out.println(s.startsWith(" "));
//        System.out.println(isok(" atsdfsdfsdf"));
        
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static String readFileByLines(String fileName,Map<String,Set<String>> map){
        System.out.println(fileName);
        
        StringBuffer buffer = new StringBuffer();

        File file = new File(fileName);
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null){
                if(!isok(tempString)){
                    continue;
                }
                
                tempString = splitStr(tempString);
                
                String[] array = tempString.split("meberId:");
                if(array.length > 1){
                    String tmp = array[1];
                    String[] arrayTmp = tempString.split("IP:");
                    
                    String ip = arrayTmp[1].split(",")[0];
                    
                    if(map.get(tmp) == null){
                        Set<String> set = new HashSet<>();
                        set.add(ip);
                        map.put(tmp, set);
                    }else{
                        map.get(tmp).add(ip);
                    }
                    
//                    if(!tmp.equals("1")){
//                        System.out.println(tmp+"--->"+ip);
//                    }
                    
//                    System.out.println("memberId:"+tmp+",ip:"+ip);
                }
                
                buffer.append(splitStr(tempString)).append("\r\n");
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if (reader != null){
                try{
                    reader.close();
                }catch (IOException e1){}
            }
        }
        return buffer.toString();
    }
    
    public static boolean isok(String str){
        
        if(str.contains("Exception")){
            return false;
        }
        if(str.contains("Log4JLogger.java:449")){
            return false;
        }
        
        if(str.length() < 8){
            return false;
        }
        
       /* String tmp = str.substring(0,8);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        try{
            df.parse(tmp);
        }catch (ParseException e){
            return false;
        }
        return true;*/
        
        str = str.replace(logTime, "");
        
        String tmp = str.substring(0,8);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        try{
            df.parse(tmp);
        }catch (ParseException e){
            return false;
        }
        return true;
    }
    
    public static String splitStr(String str){
        if(str.contains("PaymentController.java:472")){
            return str.split("\\,address")[0];
        }else if(str.contains("PaymentController.java:197")){
            return str.split("\\,result")[0];
        }else if(str.contains("PaymentController.java:250")){
            return str.split("\\,value")[0];
        }else if(str.contains("PaymentController.java:214")){
            return str.split("\\,result")[0];
        }else if(str.contains("PaymentController.java:223")){
            return str.split("\\,value")[0];
        }else if(str.contains("PaymentController.java:250")){
            return str.split("\\,value")[0];
        }
        return str;
    }
    
    public static void fw(String file ,String s){
        FileOutputStream out = null;   

        try {   

            out = new FileOutputStream(new File(file));   
                out.write(s.getBytes());   
            out.close();   


        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
